<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class District extends Model
{
    protected $table = 'districts';
    protected $fillable = ['citycode','adcode','name','center','level'];

    /**
     * 子城市
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function childs()
    {
        return $this->hasMany('App\Models\District','parent_id','id');
    }

    /**
     * 子城市
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function parent()
    {
        return $this->hasOne('App\Models\District','id','parent_id');
    }

    /**
     * 根据district表的adcode字段获取名称
     *
     * @param $code
     * @return mixed
     */
    public function getNameByAdcode($code)
    {
        $oModel = new self();
        if (is_array($code)) {
            $oSql = $oModel->whereIn('adcode', $code);
        } else {
            $oSql = $oModel->where('adcode', $code);
        }

        return $oSql->pluck('name', 'adcode')->toArray();
    }



}
